package com.hinnotek.amoeba.ui.activity.old

import android.app.Activity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.databinding.ActivityPtCreateProjectBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.ClickUtil
import com.lee.common.utils.SharedPreUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.AmibaCustomerViewModel
import com.lee.common.network.getRequestBody
import com.lee.common.network.net.ObserverState
import com.xuexiang.xutil.common.StringUtils
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class PTCreateProjectActivity : BaseActivity<ActivityPtCreateProjectBinding>() {

    val viewModel by viewModels<AmibaCustomerViewModel>()

    private var customDialog: CustomDialog? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("创建项目")

        setContentView(R.layout.activity_pt_create_project)
    }

    override fun initVew() {

        bindingView.superButtonCreate.setOnClickListener(View.OnClickListener { v: View? ->
            if (ClickUtil.isFastClick()) {
                return@OnClickListener
            }
            val projectName =
                bindingView.etPtCreateProject.text.toString().trim()
            if (StringUtils.isEmpty(projectName)) {
                ToastUtils.show(mContext, "请填写项目名称")
                return@OnClickListener
            }
            val projectExplain =
                bindingView.metProjectKeyword.editText.text.toString().trim()
            if (StringUtils.isEmpty(projectExplain)) {
                ToastUtils.show(mContext, "请填写关键字说明")
                return@OnClickListener
            }
            val customerId = intent.getIntExtra("customerId", -1)

            val orgId: Int = SharedPreUtils.getInteger("orgId", 0)
            val userName: String = SharedPreUtils.getString("userName")
            setAmoebaCustomer(
                customerId,
                projectName,
                projectExplain,
                orgId,
                userName
            )
        })
    }

    override fun setData() {}

    /**
     * app端添加项目接口
     * @param customerId
     * @param projectName
     * @param projectExplain
     * @param orgId
     * @param createUser
     */
    fun setAmoebaCustomer(
        customerId: Int,
        projectName: String,
        projectExplain: String,
        orgId: Int,
        createUser: String
    ) {
        customDialog = CustomDialog(mContext, "正在加载...")
        customDialog?.show()


        val map = mutableMapOf<String, Any>().apply {
            put("customerId", customerId)
            put("projectName", projectName)
            put("projectExplain", projectExplain)
            put("orgId", orgId)
            put("createUser", createUser)
        }
        viewModel.addProject(getRequestBody(map)).observe(this,object : ObserverState<BaseBean>(){
            override fun onDataChange(data: BaseBean) {
                setResult(Activity.RESULT_OK)
                finishActivity()
                super.onDataChange(data)
            }

            override fun onError(msg: String?, code: Int) {
                ToastUtils.showShort(mContext, msg)
                customDialog?.dismiss()
                super.onError(msg, code)
            }
        })
    }
}